<?php

require_once(dirname(__FILE__)."/BAPElement.php");
require_once(dirname(__FILE__)."/BAPTemplate.php");
require_once(dirname(__FILE__)."/BAPText.php");

/**
* Wiki Template.
	Tiene la forma {{plantilla | x | y {{valor}} | z }}
*/
class BAPArticle extends BAPElement {

	/** Llama a la funcion  general para crear el objeto */
	static public function &create(&$parser) {
		return parent::create("BAPArticle", &$parser);
	}	

	/** Representacion del objeto como una cadena */
	function __toString() {
		$ret = "";
		if ($this->name!='')
			$ret = $this->name."\n";
			
		foreach ($this->children as &$child) {
			$ret = "{$ret}".$child;
		}
		return $ret;
	}
	
	function parse() {
	
		/** Tiene la forma
			texto {{ template }} texto texto {{ template }} texto texto
		*/
		$sym = $this->parser->currentSymbol();				
		while ($sym['simbolo']!=BasicArticleParser::SYM_END) {
			switch ($sym['simbolo']) {
				case BasicArticleParser::SYM_PIPE: 
				case BasicArticleParser::SYM_TEXT:
				case BasicArticleParser::SYM_DOBLE_CLOSE:
					// tenemos que reconocer el texto 
					$this->parser->pushCurrentSymbolAsText();
					$sym = $this->parser->searchNextSymbol();
					break;
				case BasicArticleParser::SYM_DOBLE_OPEN:
					$cadenaTexto = $this->parser->popStringStack();
					if ($cadenaTexto!='') {
						$texto = BAPText::createFromString(&$this->parser,$cadenaTexto);
						if ($texto == null) {
							return false;
						}
						array_push($this->children, $texto);
					}
					// tenemos que reconocer la variable 
					$argument = BAPTemplate::create(&$this->parser);
					if ($argument == null) {
						return false;
					}
					array_push($this->children, $argument);
					break;
				case BasicArticleParser::SYM_END:
					break;
				default:
					return false;					
			}
			$sym = $this->parser->currentSymbol();
		}
		/** hay que vaciar la pila del articulo, por si acaso */
		
		$cadenaTexto = $this->parser->popStringStack();
		if ($cadenaTexto!='') {
			$texto = BAPText::createFromString(&$this->parser,$cadenaTexto);
			if ($texto == null) {
				return false;
			}
			array_push($this->children, $texto);
		}
		return true;
	}	
}

?>